<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>通用销售分析</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        //产品
        var productCBB = Ext.create('Ext.form.ComboBox', {
          store: productStore,
          fieldLabel: '产品',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'product_id',
          loadingText: '搜索中...',
          anchor: '100%',
          pageSize: 10,
          minChars: 0,
          listConfig: {
            loadingText: 'Searching...',
            getInnerTpl: function() {
              return '<div style="float:left; width:200px" data-qtip="{name}">{name:ellipsis(18)}</div><div style="float:left; width:110px">{number}</div>{is_suit}';
            }
          }
        });

        //部门
        var department = Ext.create('Ext.form.ComboBox', {
          store: departmentStore,
          fieldLabel: '业绩部门',
          blankText: "业绩部门必须选择",
          valueField: 'id',
          displayField: 'name',
          name: 'department_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',          
          loadingText: '搜索中...',
          anchor: '95%',
          readOnly:false,
          minChars: 0,
          editable:false
        });

        department.on('select', function(combo, record, index) {
          sales.reset();
          deptGroup.reset();
          salesGroup.reset();
          deptGroup.disable();
          salesGroup.disable();
        })

        //销售员
        var sales = Ext.create('Ext.form.ComboBox', {
          store: employeeStore,
          fieldLabel: '销售员',
          blankText: "销售员必须选择",
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'sales_id',          
          loadingText: '搜索中...',
          anchor: '90%',
          //pageSize: 10,
          
          minChars: 0,          
          anchor: '95%'
          //editable:false,
        });

        sales.on('select', function(combo, record, index) {
          department.reset();
          deptGroup.reset();
          salesGroup.reset();
          salesGroup.disable();
          deptGroup.disable();
        })

        var deptGroup = Ext.create('Ext.form.field.Checkbox', {
          name: 'dept_group',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '部门分组'
        })

        var salesGroup = Ext.create('Ext.form.field.Checkbox', {
          name: 'sales_group',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '销售员分组'
        })
        
        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '日期从',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });

        var endDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '至    ',labelWidth:30,
          labelWidth:30,
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var showProductInfo = Ext.create('Ext.form.field.Checkbox', {
          labelWidth:90,
          name: 'product_info',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '显示产品明细'
        })
        
        var submitButton = Ext.create('Ext.Button', {
          text: '执行统计',
          iconCls: 'finding',
          handler: function(){
            if (queryFormPanel.form.isValid()) {
              statsResultDS.removeAll();
              statsResultDS.load({params:{
                'sell_dept_id':department.getValue(),
                'product_id':productCBB.getValue(),
                'begin_date':beginDate.getRawValue(),
                'end_date':endDate.getRawValue(),
                'dept_group':deptGroup.getValue(),
                'sell_dept_id':department.getValue(),
                'sales_group':salesGroup.getRawValue(),
                'product_info':showProductInfo.getRawValue()
              }});
            }
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
            deptGroup.enable();
            salesGroup.enable();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          fieldDefaults: {
            labelWidth: 70,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1100},
              title: '统计条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [department]
                    }, {
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [sales]
                    }, {
                      columnWidth: .40,
                      layout: 'anchor',
                      xtype: 'container',
                      labelWidth: 45,
                      items: [productCBB]
                    },{
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      labelWidth: 80,
                      items: [showProductInfo]
                    }]
                },{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [deptGroup]
                    },{
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [salesGroup]
                    },{
                      columnWidth: .17,
                      layout: 'anchor',
                      xtype: 'container',
                      labelWidth: 50,
                      items: [beginDate]
                    }, {
                      columnWidth: .14,
                      layout: 'anchor',
                      xtype: 'container',
                      labelWidth: 30,
                      items: [endDate]
                    }, {
                      columnWidth: .13,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [submitButton]
                    }, {
                      columnWidth: .13,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //显示销售订单操作时的窗口介面。
        //----------------------------------销售订单Grid-------------------------
        //销售订单通用分析模型
        Ext.define('SaleOrderCommonModel', {
          extend : 'Ext.data.Model',
          fields : [{name: 'id'},
            {name: 'product_name'},
            {name: 'product_number'},
            {name: 'sell_dept_name'},
            {name: 'sales_name'},
            {name: 'quantity',type: 'int'},
            {name: 'amount',type: 'float'}
          ]
        });

        var statsResultDS =  Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleOrderCommonModel',
          proxy : {
            type : 'ajax',
            url : '/sale_order/getCommonSalesStats',
            reader : {
              type : 'json',
              root : 'root',// JSON数组对象名
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });


        //生成要订单的产品的Grid
        var statsResultGrid = Ext.create('Ext.grid.Panel', {
          title:'统计结果列表',
          store: statsResultDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 270
            },{
              header: '编号',dataIndex: 'product_number',width: 100
            },{
              header: '销售数量',
              dataIndex: 'quantity',
              align:'right',
              width: 75
            },{
              header: '销售金额',
              dataIndex: 'amount',
              align:'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 100
            },{
              header: '业绩部门',
              dataIndex: 'sell_dept_name',
              width: 75
            },{
              header: '销售员',
              dataIndex: 'sales_name',
              width: 75
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: screenHeight-350,
          frame: true,
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
          bbar: Ext.create('Ext.toolbar.Paging', {
            pageSize: pageSize,
            store: statsResultDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })          
        });    

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: screenHeight-240,
          frame:false,          
          layout: {type: 'table',columns:1},          
          items:[queryFormPanel,statsResultGrid]
        });
        workbenchPanel.render("common_sales_stats_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="common_sales_stats_workbench_panel"></div>
  </body>
</html>